const express = require('express')
const app = express()
let bodyParser = require('body-parser')
let router = express.Router()
const port = 5002

const { saveFile } = require('./service/savefile.js')
const { runCmd } = require('./service/runCmd.js')

// 解析提交的json参数
let jsonParser = bodyParser.json()
// 解析提交的form表单参数
let urlencodedParser = bodyParser.urlencoded({ extended: true })

app.use(express.static(__dirname))

router.post("/runfile",jsonParser, async (req,res,next)=>{
    // console.log(req.body)
    const result = saveFile('temp.vue', req.body.fileContent)
    await runCmd('yarn run buildfile')
    res.json({
        code: 0,
        msg: '运行成功',
        data: {
            url: '/static/online-comp.js'
        }
    })
    
})
app.use('/api', router)

app.listen(port, () => {
    console.log(`app listen at http://localhost:${port}`)
})

